package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import beans.BeanMenu;
import conexion.BDConexion;
import dao.interfaces.MenuDAO;

public class MySqlMenuDAO implements MenuDAO {

	@Override
	public List<BeanMenu> listaMenuxPerfil(String codusu) {

		List<BeanMenu> listaMenu = new ArrayList<BeanMenu>();
		BeanMenu bean = null;
		
		Connection cn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		try {
			cn = new BDConexion().obtenerConexion();
			String sql = "CALL USP_MenuxPerfil(?);";
			System.out.println("SQL: "+sql);
			pst = cn.prepareStatement(sql);
			pst.setString(1, codusu);
			
			rs = pst.executeQuery();
			
			while (rs.next()) {
				
				bean = new BeanMenu();
				bean.setIdMenu(rs.getInt(1));
				bean.setDescMenu(rs.getString(2));
				bean.setUrl(rs.getString(3));
				System.out.println(	bean.getIdMenu()+" - "+
									bean.getDescMenu()+" - "+
									bean.getUrl());
				listaMenu.add(bean);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(cn!=null)	cn.close();
				if(pst!=null)	pst.close();
				if(rs!=null)	rs.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
		return listaMenu;
		
	}

}
